** Following code draws heavily from FRB Bulletin SAS Macro: 
* https://www.federalreserve.gov/econres/files/bulletin.macro.txt 

*************************************
*** Sample and Weight Adjustments ***
*************************************
	** Only keep observations with valid ID and weight
	keep if Y1>0 & YY1>0 & X42001>0

	** Divide weight by 5 so totals estimated on the 5 implicates jointly are correct
    gen wgt=round(X42001/5)

************************
*** Add Demographics ***
************************

	** Age
	* Replace age because income earned in year t-1
	gen AGE=X14
	replace AGE=AGE-1

	** Household size
	* Marital status of the HH head: 1=married/living with partner, 2=neither married nor living with partner
	gen MARRIED = 2
	replace MARRIED = 1 if inlist(X8023, 1, 2)

	gen nhead=1
	replace nhead=2 if MARRIED == 1

	* Number of children
	gen KIDS = 0
	foreach var of varlist X108 X114 X120 X126 X132 X202 X208 X214 X220 X226 {
		replace KIDS=KIDS+(`var'==4|`var'==13|`var'==36)
	}

	** Labor force participation
	* lfp 1 = either head or spouse or both working, 0 = both head/spouse not working
	gen lfp = 0
	replace lfp = 1 if !(inrange(X4100, 50, 80) | X4100 == 97) | !(inrange(X4700, 50, 80) | X4700 == 97)

****************************
*** Add Income Variables ***
****************************

	** HH income in previous calendar year
	gen INCOME = max(0,X5729)

	* Normal income
	gen NORMINC = .
	replace NORMINC = INCOME if X7650==3
	replace NORMINC = max(0,X7362) if X7650!=3

*******************************************
***  Add Non-Financial Wealth Variables ***
*******************************************

	** Adjust for business part of farm
	replace X805=X805*((10000-X507)/10000) if X507 > 0
	replace X905=X905*((10000-X507)/10000) if X507 > 0
	replace X1005=X1005*((10000-X507)/10000) if X507 > 0
	replace X1108=X1108*((10000-X507)/10000) if X1103 == 1
	replace X1119=X1119*((10000-X507)/10000) if X1114 == 1
	replace X1130=X1130*((10000-X507)/10000) if X1125 == 1

	** Value of primary residence
	gen HOUSES=(X604+X614+X623+X716)+((10000-max(0,X507))/10000)*(X513+X526)
	replace HOUSES = 0 if HOUSES < 0

	gen HOUSECL = 2
	replace HOUSECL = 1 if inlist(X508, 1, 2)
	replace HOUSECL = 1 if inlist(X601, 1, 2, 3)
	replace HOUSECL = 1 if inlist(X701, 1, 3, 4, 5, 6, 8)
	replace HOUSECL = 1 if X701==-7 & X7133==1

**************************
*** Add Debt Variables ***
**************************

	** Mortgage
	* MRTHEL is total amount of debt secured by primary residence
	gen MRTHEL = X805+X905+X1005+.5*(max(0,X1136))*(HOUSES>0)
	replace MRTHEL = X805+X905+X1005 ///
		+X1108*(X1103==1)+X1119*(X1114==1)+X1130*(X1125==1) ///
        +max(0,X1136)*(X1108*(X1103==1)+X1119*(X1114==1) ///
		+X1130*(X1125==1))/(X1108+X1119+X1130) if (X1108+X1119+X1130)>=1

	** Add credit card variables
	gen CCBAL = max(0,X427)+max(0,X413)+max(0,X421)+max(0,X7575)
	gen hasVisa = X7973
	gen hccdebt = (CCBAL>0)
	gen ccint = X7132/100
	gen hccdebt_highint = hccdebt & (ccint>5)

	** Credit limit
	* credlimit = 0 means inappropriate (no cards) and -1 means no limit
	gen credlimit = X414
	replace credlimit = . if credlimit <= 0

	** Closeness to constraint
	gen creditremaining = credlimit - max(0,X413)

	** Update CC debt with credit card scaling
	gen ccdebt = 1.5 * CCBAL
	gen ccdebt_highint = ccdebt
	replace ccdebt_highint = 0 if ccint <= 5

	** Mortgages variables
	gen ltv = MRTHEL/HOUSES
	gen HOMEEQ = HOUSES - MRTHEL

***********************
*** Calculate Taxes ***
***********************

***	Apply tax brackets in survey year - 1, when income was earned
**	All income is nominal

	** File type
	* Roughly characterize as filing singly (1), jointly (2), or head of house (3)
	gen filetype = 0
	replace filetype = 1 if (nhead == 1 & KIDS == 0)
	replace filetype = 2 if nhead == 2
	replace filetype = 3 if nhead == 1 & KIDS > 0

	** Standard deduction
	gen stddeduction = 0
	replace stddeduction = 6300 if filetype == 1
	replace stddeduction = 12600 if filetype == 2
	replace stddeduction = 9250 if filetype == 3

	** Personal exemption
	gen exemption = 0
	replace exemption = 4000*(nhead + KIDS)

	** Apply federal taxes
	gen taxableincome = max(0, NORMINC - stddeduction - exemption)
	gen fedtaxes = 0

	local b1 9225
	local b2 37450
	local b3 90750
	local b4 189300
	local b5 411500
	local b6 413200
	replace fedtaxes = 0.10*(min(taxableincome,`b1')) if filetype == 1
	replace fedtaxes = fedtaxes + 0.15*(min(taxableincome-`b1',`b2'-`b1')) if (filetype == 1 & taxableincome > `b1')
	replace fedtaxes = fedtaxes + 0.25*(min(taxableincome-`b2',`b3'-`b2')) if (filetype == 1 & taxableincome > `b2')
	replace fedtaxes = fedtaxes + 0.28*(min(taxableincome-`b3',`b4'-`b3')) if (filetype == 1 & taxableincome > `b3')
	replace fedtaxes = fedtaxes + 0.33*(min(taxableincome-`b4',`b5'-`b4')) if (filetype == 1 & taxableincome > `b4')
	replace fedtaxes = fedtaxes + 0.35*(min(taxableincome-`b5',`b6'-`b5')) if (filetype == 1 & taxableincome > `b5')
	replace fedtaxes = fedtaxes + 0.396*(taxableincome-`b6') if (filetype == 1 & taxableincome > `b6')

	local b1 18450
	local b2 74900
	local b3 151200
	local b4 230450
	local b5 411500
	local b6 464850
	replace fedtaxes = 0.10*(min(taxableincome,`b1')) if filetype == 2
	replace fedtaxes = fedtaxes + 0.15*(min(taxableincome-`b1',`b2'-`b1')) if (filetype == 2 & taxableincome > `b1')
	replace fedtaxes = fedtaxes + 0.25*(min(taxableincome-`b2',`b3'-`b2')) if (filetype == 2 & taxableincome > `b2')
	replace fedtaxes = fedtaxes + 0.28*(min(taxableincome-`b3',`b4'-`b3')) if (filetype == 2 & taxableincome > `b3')
	replace fedtaxes = fedtaxes + 0.33*(min(taxableincome-`b4',`b5'-`b4')) if (filetype == 2 & taxableincome > `b4')
	replace fedtaxes = fedtaxes + 0.35*(min(taxableincome-`b5',`b6'-`b5')) if (filetype == 2 & taxableincome > `b5')
	replace fedtaxes = fedtaxes + 0.396*(taxableincome-`b6') if (filetype == 2 & taxableincome > `b6')

	local b1 13150
	local b2 50200
	local b3 129600
	local b4 209850
	local b5 411500
	local b6 439000
	replace fedtaxes = 0.10*(min(taxableincome,`b1')) if filetype == 3
	replace fedtaxes = fedtaxes + 0.15*(min(taxableincome-`b1',`b2'-`b1')) if (filetype == 3 & taxableincome > `b1')
	replace fedtaxes = fedtaxes + 0.25*(min(taxableincome-`b2',`b3'-`b2')) if (filetype == 3 & taxableincome > `b2')
	replace fedtaxes = fedtaxes + 0.28*(min(taxableincome-`b3',`b4'-`b3')) if (filetype == 3 & taxableincome > `b3')
	replace fedtaxes = fedtaxes + 0.33*(min(taxableincome-`b4',`b5'-`b4')) if (filetype == 3 & taxableincome > `b4')
	replace fedtaxes = fedtaxes + 0.35*(min(taxableincome-`b5',`b6'-`b5')) if (filetype == 3 & taxableincome > `b5')
	replace fedtaxes = fedtaxes + 0.396*(taxableincome-`b6') if (filetype == 3 & taxableincome > `b6')

	** Apply state taxes
	gen statetaxes = 0.05*NORMINC

	** Social security and medicare tax
	gen medicaretax = 0.0145*NORMINC
	gen socsectax = 0.062*min(NORMINC,118500)

	** Add after-tax variables
	gen taxes = fedtaxes + statetaxes + medicaretax + socsectax
	gen atincome = NORMINC - taxes

******************
*** Adjust CPI ***
******************

*** The CPI for income is shifted by 1 year since income is in period t-1 dollars
	local CPILAG 3526/3482

	foreach var of varlist INCOME NORMINC atincome {
		replace `var' = `var' * `CPILAG'
	}

***********************************
*** Add Income-Scaled Variables ***
***********************************

	gen HOUSESscaled = HOUSES/atincome
	gen ccdebtscaled = ccdebt/atincome
	gen ccdebt_highintscaled = ccdebt_highint/atincome
	gen credlimitscaled = credlimit/atincome
	gen creditremainingscaled = creditremaining/atincome
	gen closetoconstraint = (creditremainingscaled<1/26) & hccdebt==1


	drop X* J*
	